import request from '@/utils/request'
import type { ApiResponse } from '@/types/auth'

// 分类接口类型定义
export interface Category {
  _id: string
  name: string
  type: 'subject' | 'grade' | 'courseType'
  description?: string
  order: number
  isActive: boolean
  createdAt: string
  updatedAt: string
}

export interface CategoryListResponse {
  categories: Category[]
  grouped: {
    subject?: Category[]
    grade?: Category[]
    courseType?: Category[]
  }
}

export interface CategoryStatsResponse {
  subjects: Array<{
    _id: string
    name: string
    count: number
    percentage: number
    avgViews: number
    avgLikes: number
  }>
  grades: Array<{
    _id: string
    name: string
    count: number
    percentage: number
  }>
  courseTypes: Array<{
    _id: string
    name: string
    count: number
    percentage: number
  }>
  tags: Array<{
    tag: string
    count: number
  }>
  summary: {
    totalLessonPlans: number
    totalSubjects: number
    totalGrades: number
    totalCourseTypes: number
    totalTags: number
  }
}

// 分类API接口
export const categoryApi = {
  // 获取所有分类
  getAll(type?: string): Promise<ApiResponse<CategoryListResponse>> {
    return request.get('/categories', { params: { type } })
  },

  // 获取分类统计
  getCategoryStats(): Promise<ApiResponse<CategoryStatsResponse>> {
    return request.get('/lessonplans/category-stats')
  },

  // 获取热门搜索
  getHotSearches(limit = 10): Promise<ApiResponse<Array<{ keyword: string; count: number }>>> {
    return request.get('/lessonplans/hot-searches', { params: { limit } })
  }
}